﻿using B2B.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace B2B.Views
{
    public partial class GrupTanitim : ChildWindow
    {
        DataServiceClient client = new DataServiceClient();

        public GrupTanitim()
        {
            InitializeComponent();
            this.Loaded += GrupTanitim_Loaded;
            this.lstGruplar.SelectionChanged += lstGruplar_SelectionChanged;
        }

        void lstGruplar_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstGruplar.SelectedIndex > -1)
            {
                KullaniciGrup grp = lstGruplar.SelectedItem as KullaniciGrup;
                if (grp != null)
                {
                    txtGrupAdi.Text = grp.Adi;
                    txtGrupAdi.Tag = grp.ID;
                }
            }
        }

        void GrupTanitim_Loaded(object sender, RoutedEventArgs e)
        {
            client.GrupEkleCompleted += client_GrupEkleCompleted;
            client.GrupSilCompleted += client_GrupSilCompleted;
            client.GrupListeleCompleted += client_GrupListeleCompleted;
            client.GrupListeleAsync();
        }

        void client_GrupListeleCompleted(object sender, GrupListeleCompletedEventArgs e)
        {
            lstGruplar.ItemsSource = e.Result;
            lstGruplar.DisplayMemberPath = "Adi";
        }

        void client_GrupSilCompleted(object sender, GrupSilCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Silindi !");
            }
            else
            {
                MessageBox.Show("Silinemedi !");
            }

            txtGrupAdi.Text = "";
            txtGrupAdi.Tag = null;
            client.GrupListeleAsync();
        }

        void client_GrupEkleCompleted(object sender, GrupEkleCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Eklendi !");
            }
            else
            {
                MessageBox.Show("eklenemedi !");
            }
            txtGrupAdi.Text = "";
            txtGrupAdi.Tag = null;
            client.GrupListeleAsync();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtGrupAdi.Text))
            {
                int id = -1;
                if (txtGrupAdi.Tag != null)
                {
                    id = Convert.ToInt32(txtGrupAdi.Tag);
                }
                client.GrupEkleAsync(id, txtGrupAdi.Text);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtGrupAdi.Text) && txtGrupAdi.Tag != null)
            {
                int id = -1;
                if (txtGrupAdi.Tag != null)
                {
                    id = Convert.ToInt32(txtGrupAdi.Tag);
                }
                client.GrupSilAsync(id);
            }
        }

        private void YeniButton_Click(object sender, RoutedEventArgs e)
        {
            txtGrupAdi.Text = "";
            txtGrupAdi.Tag = null;
            txtGrupAdi.Focus();
        }
    }
}

